package helpers;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class PaginatorHelper {
	
	private TreeMap params = new TreeMap();
	
	private String url = "";
	
	private int current = 1;
	
	private int limitPage = 5;

	public PaginatorHelper(String queryString) {
		if (queryString != null) {
			queryString = queryString.replaceAll("^[^?]+[?]", "");
			String p[] = queryString.split("&");
			for (String arg : p) {
				if (!arg.isEmpty()) {
					String s[] = arg.split("=");
					this.params.put(s[0], s[1]);
					if (s[0].equalsIgnoreCase("page")) {
						current = Integer.parseInt(s[1]);
					}
				}
			}
		}
	}
	
	public TreeMap getParams() {
		return params;
	}

	public void setParams(TreeMap params) {
		this.params = params;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current;
	}
	
	public int getLimitPage() {
		return limitPage;
	}

	public void setLimitPage(int limitPage) {
		this.limitPage = limitPage;
	}

	public void addParam(String key, String value) {
		this.params.put(key, value);
	}
	
	public List<Integer> getRangePage() {
		List<Integer> range = new ArrayList<Integer>();
		
		int step = (int) limitPage/2;
		if (limitPage%2==0) 
			step = step - 1;
		
		int start = current - step;
		if (start <= 1) {
			start = 1;
		}
		
		int end = start + limitPage;
		
		for (int i = start; i < end; i++) {
			range.add(i);
		}
		
		return range;
	}
	
	public String getNext() {
		String next = url + "?";
		
		Set keys = params.keySet();
		boolean existsPage = false;
		
		for (Iterator i = keys.iterator(); i.hasNext();) {
			String key = (String) i.next();
			String value = (String) params.get(key);
			if (key.equalsIgnoreCase("page")) {
				existsPage = true;
				value = Integer.toString(Integer.parseInt(value) + 1);
			}
			
			next += "&" + key + "=" + value;
		}
		
		if (!existsPage) {
			next += "&page=" + Integer.toString(current + 1);
		}

		return next;
	}
	
	public String getPrev() {
		String prev = url + "?";
		
		Set keys = params.keySet();
		boolean existsPage = false;
		
		for (Iterator i = keys.iterator(); i.hasNext();) {
			String key = (String) i.next();
			String value = (String) params.get(key);
			if (key.equalsIgnoreCase("page")) {
				existsPage = true;
				value = Integer.toString(Integer.parseInt(value) - 1);
			}
			
			prev += "&" + key + "=" + value;
		}
		
		if (!existsPage) {
			prev += "&page=" + Integer.toString(current - 1);
		}

		return prev;
	}
	
	public String getPage(int page) {
		String strPage = url + "?";
		
		Set keys = params.keySet();
		boolean existsPage = false;
		
		for (Iterator i = keys.iterator(); i.hasNext();) {
			String key = (String) i.next();
			String value = (String) params.get(key);
			if (key.equalsIgnoreCase("page")) {
				existsPage = true;
				value = Integer.toString(page);
			}
			
			strPage += "&" + key + "=" + value;
		}
		
		if (!existsPage) {
			strPage += "&page=" + Integer.toString(page);
		}

		return strPage;
	}
	
	public static void main(String[] args) {
		PaginatorHelper paginator = new PaginatorHelper("");
		paginator.setCurrent(4);
		paginator.setLimitPage(6);
		paginator.getRangePage();
	}
}
